<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>工序物料设计单</title>
</head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css"
      rel="stylesheet">
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-table.min.css">
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="../js/bootstrap-table.min.js"></script>
<script src="../js/bootstrap-table-zh-CN.min.js"></script>


<body>

<div class="modal fade m-auto" id="myModal">
    <div class="modal-dialog modal-xl" style="width: 1000px;height: 800px">
        <div class="modal-content">
            <!--     制作设计单模态框       -->
            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">生产工序物料设计单</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <div class="m-auto row">
                    <!-- 父级编号 -->
                    <span id="parentId" style="display: none"></span>

                    <div class="col"><span>设计单编号:</span><span id="id" class=""></span></div>
                    <div class="col"><span>设计人:</span><span id="designer"></span></div>
                </div>
                <div class="m-auto row" style="padding-top: 15px">
                    <div class="col"><span class="">产品名称:</span><span id="name"></span></div>
                    <div class="col"><span class="-text">产品编号:</span><span id="productId"></span></div>
                </div>
                <div style="padding-top: 15px">
                    <table id="tab1" class="table table-striped table-bordered table-hover">

                    </table>
                </div>
                <div class="row">
                    <div class="col"><span class="-text">工时成本:</span><span id="costPriceSum"></span></div>
                    <div class="col"><span class="">物料总成本:</span><span id="moduleCostPriceSum"></span></div>
                </div>

                <div class="m-auto row" style="padding-top: 15px">
                    <div class="col"><span class="">审核人:</span><span id="person"></span></div>
                    <div class="col"><span class="">审核时间:</span><span id="checkTime"></span></div>
                </div>
                <div class="row m-auto" style="padding-top: 15px">
                    <span class="col-3  ">设计要求:</span><textarea id="procedureDescribe"
                                                                style="width: 500px; height: 100px;"></textarea>
                </div>
            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <button type="button" onclick="MDesignProcedureObj.UpdateDModuleDetailsTag()" class="btn btn-primary">
                    提交
                </button>
            </div>


        </div>
    </div>
</div>


<!-- 工序物料模态框 -->
<div class="modal fade" id="myModal1">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">工序物料设计单</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <span id="jianjie"></span>
                <table id="tab2">

                </table>
            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <button type="button" onclick="MDesignProcedureObj.AddDModuleDetailsText()"
                        class="btn btn-primary">提交
                </button>
            </div>

        </div>
    </div>
</div>

<!-- 工序物料模态框 -->
<div class="modal fade" id="myModal2">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">工序物料设计单</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">

                <div class="m-auto row">
                    <!-- 父级编号 -->
                    <span id="" style="display: none"></span>

                    <div class="col"><span>工序单编号:</span><span id="ProcessId" class=""></span></div>
                    <div class="col"><span>工序名称:</span><span id="ProcessName"></span></div>
                </div>
                <div class="m-auto row" style="padding-top: 15px">
                    <div class="col"><span class="">登记人:</span><span id="Process"></span></div>
                    <div class="col"><span class="-text">登记时间:</span><span id="time"></span></div>
                </div>
            </div>
            <div style="padding-top: 15px">

                <table id="tab3" class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>物料名称</th>
                        <th>物料编号</th>
                        <th>描述</th>
                        <th>本工序数量</th>
                        <th>单位</th>
                        <th>单价(元)</th>
                        <th>小计(元)</th>
                    </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <button type="button" onclick="MDesignProcedureObj.AddDModuleDetailsSum()" class="btn btn-primary">提交
                </button>
            </div>

        </div>
    </div>
</div>


<nav class="breadcrumb">
    <a class="breadcrumb-item" href="#">生产管理</a>
    <a class="breadcrumb-item" href="#">产品工序设计</a>
    <a class="breadcrumb-item" active href="#">制定产品生产工序设计单</a>
</nav>

<div class="input-group mb-3" style="width: 50%">
    <div class="input-group-prepend">
        <span class="input-group-text">产品编号</span>
    </div>
    <input id="designId" type="text" class="form-control">
    &nbsp;
    <div class="input-group-prepend">
        <span class="input-group-text">产品名称</span>
    </div>
    <input id="productName" type="text" class="form-control">
    &nbsp;
    <button onclick="$('#tab').bootstrapTable('refresh')" class="btn btn-primary">查询</button>
</div>

<div>
    <table id="tab">

    </table>
</div>
</body>

<script>
    var tab;
    var cont = 0;
    var MDesignProcedureObj = {

        MDesignProcedureText: function (MDesignProcedure) {
            $('#tab1').bootstrapTable('destroy');

            $("#id").text(MDesignProcedure.designId)//设计单编号
            $("#designer").text(MDesignProcedure.designer)//设计人
            $("#name").text(MDesignProcedure.productName)//产品名称
            $("#productId").text(MDesignProcedure.productId)//产品编号
            $("#costPriceSum").text(MDesignProcedure.costPriceSum)//工时成本
            $("#checkTime").text(new Date().toLocaleString())//审核时间
            $("#procedureDescribe").val(MDesignProcedure.procedureDescribe)//设计要求
            $("#jianjie").text("制造" + MDesignProcedure.productName + "产品,供选择的物料如下:")//设计要求
            $("#parentId").text(MDesignProcedure.id)
            $("#moduleCostPriceSum").text(MDesignProcedure.moduleCostPriceSum)


            $.ajax({
                type: "post",
                url: "/user.action",
                success: function (v) {
                    $("#person").text(v.loginId);
                    $("#Process").text(v.loginId)
                }
            })
            $("#tab1").bootstrapTable('destroy')

            $("#tab1").bootstrapTable({
                method: "get",
                url: "/queryMDesignProcedureDetailsByParentId.action",
                striped: true,
                queryParams: function (params) {
                    return {
                        id: MDesignProcedure.id
                    }
                },
                columns: [
                    {
                        title: "序号", field: "id", formatter: function (value, row, index) {

                            return "<span>" + value + "</span>"
                        }
                    },
                    {title: "工序名称", field: "procedureName"},
                    {title: "工序编号", field: "procedureId"},
                    {
                        title: "描述", field: "procedureDescribe", formatter: function (value, row, index) {
                            return "<span style='width: 100px'>" + value + "</span>"
                        }
                    },
                    {
                        title: "工时数", field: "labourHourAmount", formatter: function (value, row, index) {
                            return "<span id='" + "labourHourAmount" + row.id + "' class='calculate' type='number' style='width: 100px'>" + value + "</span>"
                        }
                    },

                    {
                        title: "单位工时成本", field: "costPrice", formatter: function (value, row, index) {
                            return "<span id='" + "costPrice" + row.id + "'type='number' style='width: 100px' >" + value + "</span>"
                        }
                    },
                    {
                        title: "工时成本小计(元)", field: "subtotal", formatter: function (value, row, index) {
                            var span = "<span id='" + row.id + "'>" + value + "</span>"
                            return span;

                        }
                    },
                    {
                        title: "设计", field: "id", align: "center", formatter: function (value, row, index) {
                            if (row.designModuleTag == 'D002-1') {
                                return "<button id='" + row.id + "' onclick='MDesignProcedureObj.QueryMDesignProcedureModule(" + JSON.stringify(row) + ",this)' class='btn btn-primary'>重新设计</button>"
                            } else {
                                return "<button id='" + row.id + "' onclick='MDesignProcedureObj.QueryMDesignProcedureModule(" + JSON.stringify(row) + ",this)' class='btn btn-primary' data-toggle=\"modal\" data-target=\"#myModal1\">设计</button>"
                            }
                        }
                    }
                ]
            })


        },
        QueryMDesignProcedureModule: function (MDesignProcedure, i) {

            $('#tab2').bootstrapTable('destroy');
            tab = i;
            $("#ProcessId").text(MDesignProcedure.procedureId)
            $("#ProcessName").text(MDesignProcedure.procedureName)
            $("#time").text(new Date().toLocaleString())
            $.ajax({
                type: "post",
                url: "/queryDesignModuleByproductId.action",
                data: "parenId=" + $("#productId").text(),
                success: function (DesignModule) {
                    $('#tab2').bootstrapTable('destroy');
                    console.log(DesignModule)
                    $("#tab2").bootstrapTable({
                        method: "get",
                        url: "/queryDModuleDetailsByParentId.action",
                        striped: true,
                        queryParams: function (params) {
                            return {
                                parentId: DesignModule.id
                            }
                        }, columns: [
                            {title: "序号", field: "id"},
                            {title: "物料名称", field: "productName"},
                            {
                                title: "物料编号", field: "productId"
                            },
                            {title: "描述", field: "productDescribe"},
                            {title: "设计数量", field: "amount"},
                            {title: "可用数量", field: "residualAmount"},
                            {title: "单位", field: "amountUnit"},
                            {
                                title: "单价(元)", field: "costPrice",
                                formatter: function (value, row, index) {
                                    return "<span>" + value + "</span>"
                                }
                            },
                            {
                                title: "本工序数量", formatter: function (value, row, index) {
                                    return "<input  type='number' value=0>"
                                }
                            },
                        ]

                    })
                }
            })
            if ($(i).text() === "重新设计") {
                if (confirm("重新设计将清除之前的设计")) {
                    $('#tab2').bootstrapTable('destroy');
                    $("#myModal1").modal("show");
                    $.ajax({
                        type: "post",
                        url: "/queryMDesignProcedureModuleByParentId.action",
                        data: "id=" + $(i).parent().parent().eq(0).children().eq(0).text(),
                        success: function (MDesignProcedureModule) {
                            console.log(MDesignProcedureModule)
                            $.ajax({
                                type: "post",
                                url: "/queryDesignModuleByproductId.action",
                                data: "parenId=" + $("#productId").text(),
                                success: function (DesignModule) {
                                    var dModuleDetails = []
                                    $.each(MDesignProcedureModule, function (i, v) {
                                        var a = {}
                                        var amount = MDesignProcedureModule[i].amount
                                        var productName = MDesignProcedureModule[i].productName
                                        var parentId = DesignModule.id
                                        a.amount = amount
                                        a.productName = productName
                                        a.parentId = parentId
                                        dModuleDetails.push(a)
                                    })
                                    $.ajax({
                                        type: "post",
                                        url: "/deleteDesignModuleByParentId.action",
                                        data: "parentId=" + $(i).parent().parent().eq(0).children().eq(0).text(),
                                        success: function (z) {
                                        }
                                    })

                                    console.log(dModuleDetails)
                                    $.ajax({
                                        type: "post",
                                        url: "/redesign.action",
                                        data: JSON.stringify(dModuleDetails),
                                        contentType: "application/json;charset=utf-8",
                                        success: function (i) {
                                            $("#tab1").bootstrapTable("refresh")
                                            $("#tab2").bootstrapTable("refresh")
                                        }
                                    })
                                }
                            })
                        }
                    })
                } else {
                    $("#myModal2").modal("hide");
                }

            }


        },

        AddDModuleDetailsText: function () {

            var result = []
            $.each($("#tab2>tbody>tr"), function (i, v) {
                var DModuleDetails = {}
                var id = $("#tab1>tbody>tr").eq(0).children().eq(0).text()//获取id
                var parentId = $("#tab2>tbody>tr").eq(i).children().eq(0).text()//获取id
                var amount2 = $("#tab2>tbody>tr").eq(i).find("input[type='number']").val()//获取本工序数量
                var amount1 = $("#tab2>tbody>tr").eq(i).find("span").text()
                var subtotal = parseInt(amount2) * parseInt(amount1)//获取小计
                var productName = $("#tab2>tbody>tr").eq(i).children().eq(1).text()//获取物料名字
                var productId = $("#tab2>tbody>tr").eq(i).children().eq(2).text()//获取物料编号
                var productDescribe = $("#tab2>tbody>tr").eq(i).children().eq(3).text()//获取描述
                var amount = $("#tab2>tbody>tr").eq(i).children().eq(4).text()//获取设计数量
                var residualAmount = $("#tab2>tbody>tr").eq(i).children().eq(5).text()//获取可用数量
                var amountUnit = $("#tab2>tbody>tr").eq(i).children().eq(6).text()//获取单位
                var costPrice = $("#tab2>tbody>tr").eq(i).children().eq(7).find("span").text()//获取单价

                DModuleDetails.id = id
                DModuleDetails.parentId = parentId
                DModuleDetails.amount2 = amount2
                DModuleDetails.amount = amount
                DModuleDetails.subtotal = subtotal
                DModuleDetails.productName = productName
                DModuleDetails.productDescribe = productDescribe
                DModuleDetails.residualAmount = residualAmount
                DModuleDetails.costPrice = costPrice
                DModuleDetails.amountUnit = amountUnit
                DModuleDetails.productId = productId

                result.push(DModuleDetails)
            })

            var t1 = "";
            var t2 = "";
            $.each(result, function (i, v) {
                //传值到另一个表
                if (parseInt(result[i].amount2) > parseInt(result[i].residualAmount)) {
                    t1 = "本工序数量大于可用数量";
                } else if (isNaN(parseInt(result[i].amount2))) {
                    t2 = "请输入本工序数量";
                } else {
                    $("#myModal2").modal("show");
                    $("#tab3>tbody").html("")
                    $.each(result, function (i, v) {
                        $("#tab3>tbody").append("<tr>" +
                            "<td>" + result[i].parentId + "</td>" +
                            "<td>" + result[i].productName + "</td>" +
                            "<td>" + result[i].productId + "</td>" +
                            "<td>" + result[i].productDescribe + "</td>" +
                            "<td>" + result[i].amount2 + "</td>" +
                            "<td>" + result[i].amountUnit + "</td>" +
                            "<td>" + result[i].costPrice + "</td>" +
                            "<td>" + result[i].subtotal + "</td>" +
                            "</tr>")
                    })
                }
            })
            if (t1 != "") {
                alert(t1);
            }
            if (t2 != "") {
                alert(t2);
            }

        },
        AddDModuleDetailsSum: function () {
            var result = [];

            $.each($("#tab3>tbody>tr"), function (i, v) {
                var MDesignProcedureModule = {}
                var amount = $("#tab3>tbody>tr").eq(i).children().eq(4).text()//本工序数量
                var subtotal = $("#tab3>tbody>tr").eq(i).children().eq(7).text()//小计
                var parentId = $(tab).parent().parent().eq(0).children().eq(0).text()//父级id
                var id = $("#tab3>tbody>tr").eq(i).children().eq(0).text()//id
                var productId = $("#tab3>tbody>tr").eq(i).children().eq(2).text()
                var productName = $("#tab3>tbody>tr").eq(i).children().eq(1).text()
                var productDescribe = $("#tab3>tbody>tr").eq(i).children().eq(3).text()
                var amountUnit = $("#tab3>tbody>tr").eq(i).children().eq(5).text()
                var costPrice = $("#tab3>tbody>tr").eq(i).children().eq(6).text()

                MDesignProcedureModule.amount = amount
                MDesignProcedureModule.subtotal = subtotal
                MDesignProcedureModule.parentId = parentId
                MDesignProcedureModule.id = id
                MDesignProcedureModule.productId = productId
                MDesignProcedureModule.productName = productName
                MDesignProcedureModule.productDescribe = productDescribe
                MDesignProcedureModule.amountUnit = amountUnit
                MDesignProcedureModule.costPrice = costPrice

                result.push(MDesignProcedureModule)
            })
            var mDesignProcedureModules = {
                mDesignProcedureModules: result
            }
            var count = 0;
            $.each(result, function (i, v) {
                count += parseInt(result[i].subtotal)
            })
            var a = {
                id: $(tab).parent().parent().eq(0).children().eq(0).text(),
                moduleSubtotal: count
            }
            $.ajax({
                type: "post",
                url: "/updateMDesignProcedureDetailsModuleSubtotal.action",
                data: JSON.stringify(a),
                contentType: "application/json;charset=utf-8",
                subtotal: function (i) {

                }
            })

            // console.log(JSON.stringify(result))
            console.log(JSON.stringify(mDesignProcedureModules))
            //批量添加物料
            $.ajax({
                type: "post",
                url: "/insertDModuleDetails.action",
                data: JSON.stringify(result),
                contentType: "application/json",
                success: function (i) {
                    if (i > 0) {
                        //重新制定加上上次制定的本工序数量
                        $.ajax({
                            type: "post",
                            url: "/updateDModuleDetails.action",
                            data: JSON.stringify(result),
                            contentType: "application/json;charset=utf-8",
                            success: function (i) {
                                var result = []
                                $.each($("#tab1>tbody>tr"), function (index, v) {
                                    var mDesignProcedureDetailsList = {}
                                    id = $("#tab1>tbody>tr").eq(index).children().eq(0).text()
                                    mDesignProcedureDetailsList.id = id;
                                    result.push(mDesignProcedureDetailsList)
                                })

                                //物料总成本
                                $.ajax({
                                    type: "post",
                                    url: "/queryMDesignProcedureDetailsById.action",
                                    data: JSON.stringify(result),
                                    contentType: "application/json",
                                    success: function (i) {
                                        var count = 0;
                                        $.each(i, function (index, value) {
                                            count += i[index].moduleSubtotal
                                        })
                                        var mDesignProcedure = {
                                            id: $("#parentId").text(),
                                            moduleCostPriceSum: count
                                        }
                                        $.ajax({
                                            type: "post",
                                            url: "/updateMDesignProcedureModuleCostPriceSum.action",
                                            data: JSON.stringify(mDesignProcedure),
                                            contentType: "application/json",
                                            success: function (i) {
                                                $("#moduleCostPriceSum").text(count)
                                            }
                                        })
                                    }
                                })
                                if (i > 0) {
                                    //制定后修改状态
                                    $.ajax({
                                        type: "post",
                                        url: "/updateMDesignProcedureDetailsTagById.action",
                                        data: "id=" + parseInt($(tab).parent().parent().eq(0).children().eq(0).text()),
                                        success: function (i) {


                                            if (i > 0) {
                                                $("#tab1").bootstrapTable("refresh")
                                                $("#tab2").bootstrapTable("refresh")
                                                $("#tab3").bootstrapTable("refresh")
                                                $("#myModal2").modal("hide");
                                                $("#myModal1").modal("hide");
                                            }

                                        }
                                    })
                                }
                            }
                        })
                    }
                }
            })

        },
        UpdateDModuleDetailsTag: function () {
            $.ajax({
                type: "post",
                url: "/queryDModuleDetailsByproductId.action",
                data: "productId=" + $("#productId").text(),
                success: function (i) {
                    console.log(i.id)
                    $.ajax({
                        type: "post",
                        url: "/queryDModuleDetailsByParentId.action",
                        data: "parentId=" + i.id,
                        success: function (i) {
                            var DModuleDetails = []
                            $.each(i, function (index, n) {
                                var b = {}
                                var residualAmount = i[index].residualAmount

                                b.residualAmount = residualAmount
                                DModuleDetails.push(b)
                            })
                            var MDesignProcedure = {
                                productId: $("#productId").text(),
                                moduleCostPriceSum: $("#moduleCostPriceSum").text()
                            }
                            $.ajax({
                                type: "post",
                                url: "/updateMDesignProcedureModuleCostPriceSum.action",
                                data: JSON.stringify(MDesignProcedure),
                                contentType: "application/json;charset=utf-8",
                                success: function (i) {

                                }
                            })
                            let text = "";
                            let text1 = "";
                            $.each(DModuleDetails, function (i, v) {

                                if (DModuleDetails[i].residualAmount > 0) {
                                    text = "物料还未使用完";
                                } else if(text==""){
                                        $.ajax({
                                            type: "post",
                                            url: "/updateDesignModuleTagById.action",
                                            data: "id=" + parseInt($("#parentId").text()),
                                            success: function (l) {
                                                alert("提交成功")
                                                text1="提交成功";
                                                $("#myModal").modal("hide")
                                                $("#tab").bootstrapTable("refresh")
                                            }
                                        })

                                }
                            })
                            if (text1 != "") {
                                alert(text1);
                            }
                            if (text != "") {
                                alert(text);
                            }
                        }
                    })
                }
            })

        }


    }


</script>

<script>
    $(function () {

        $("#tab").bootstrapTable({
            method: "post",
            url: "/queryByDesignModuleTag.action",
            striped: true,
            pageNumber: 1,
            height: 600,
            pagination: true,
            sidePagination: "server",
            pageSize: 3,
            pageList: [3, 6, 9],
            queryParams: function (i) {
                return {
                    //计算当前页
                    // currentPage:(parts.offset/parts.limit)+1,
                    pageNumber: i.offset,
                    //每条页数
                    pageSize: i.limit,
                    productId: $("#designId").val(),
                    productName: $("#productName").val()
                }
            },
            columns: [
                {title: "工序设计单编号", field: "designId"},
                {title: "产品编号", field: "productId"},
                {title: "产品名称", field: "productName"},
                {title: "设计人", field: "designer"},
                {title: "登记时间", field: "registerTime"},
                {title: "工时总成本", field: "costPriceSum"},
                {
                    title: "制定", field: "id", align: "center",
                    formatter: function (value, row, index) {
                        return "<button onclick='MDesignProcedureObj.MDesignProcedureText(" + JSON.stringify(row) + ")' class=\"btn btn-primary btn-sm\" data-toggle=\"modal\" data-target=\"#myModal\">制定</button>";
                    }
                }
            ]
        })
    })


</script>
</html>